package com.jyniubi.gmail.index.controller;

import com.jyniubi.gmail.common.bean.ResponseVo;
import com.jyniubi.gmail.index.service.IndexService;
import com.jyniubi.gmail.pms.entity.CategoryEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class IndexController {
    @Resource
    private IndexService indexService;


    @GetMapping("/index/cates/{cid}")
    @ResponseBody
    public ResponseVo<List<CategoryEntity>> queryLvl2CategoriesWithSubs(@PathVariable("cid") Long cid) {
        List<CategoryEntity> categoryEntities = this.indexService.queryLvl2CategoriesWithSubs(cid);
        return ResponseVo.ok(categoryEntities);
    }

    @GetMapping("/**")
    public String toIndex(Model model) {
        List<CategoryEntity> categories = this.indexService.queryLvl1Categories();
        model.addAttribute("categories", categories);
        return "index";
    }

// --------------------测试---------------------------------

    @ApiOperation("测试本地锁和集群本地锁")
    @GetMapping("/index/lock")
    @ResponseBody
    public ResponseVo testLock() {
        this.indexService.testLock();
        return ResponseVo.ok();
    }

    @GetMapping("/index/write")
    @ResponseBody
    public ResponseVo testWrite() {
        this.indexService.testWrite();
        return ResponseVo.ok("这是测试写操作");
    }

    @GetMapping("/index/read")
    @ResponseBody
    public ResponseVo testRead() {
        this.indexService.testRead();
        return ResponseVo.ok("这是测试读操作");
    }


    @GetMapping("/index/latch")
    @ResponseBody
    public ResponseVo testLatch() {
        this.indexService.testLatch();
        return ResponseVo.ok("班长锁门...");
    }

    @GetMapping("/index/countDown")
    @ResponseBody
    public ResponseVo testCountDown() {
        this.indexService.testCountDown();
        return ResponseVo.ok("出来一个同学");
    }

    @GetMapping("/index/semaphore")
    @ResponseBody
    public ResponseVo testSemaphore() {
        this.indexService.testSemaphore();
        return ResponseVo.ok("有车位了");
    }
}
